package com.example.hotelsystem.controller.city;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hotelsystem.entity.City;
import com.example.hotelsystem.entity.CityImage;
import com.example.hotelsystem.entity.User;
import com.example.hotelsystem.entity.vo.CitySearchVO;
import com.example.hotelsystem.page.Pagesize;
import com.example.hotelsystem.result.Result;
import com.example.hotelsystem.service.CityImageService;
import com.example.hotelsystem.service.CityService;
import com.example.hotelsystem.vo.city.CityViewVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lee
 * @since 2021-03-07
 */
@RestController
@RequestMapping("/hotelsystem/city")
@Api(tags = "城市信息接口控制器")
public class CityController {

    Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);

    @Autowired
    CityService cityService;

    @Autowired
    CityImageService cityImageService;

    @PostMapping("/list")
    @ApiOperation("城市列表")
    public Result CityList(CitySearchVO citySerachVO){
        try{
            IPage<City> citys=cityService.CitySearchList(citySerachVO);
            if(citys!=null){
                return Result.success(citys);
            }
            return Result.error("找不到城市");
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/remove")
    @ApiOperation("删除城市")
    public Result RemoveCity(Long cityId){
        try{
            City c = cityService.getById(cityId);
            c.setIsDelete(1);
            boolean isRemove = cityService.updateById(c);
            if(isRemove){
                return Result.success("操作成功");
            }
            return Result.error("删除失败");
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/mod")
    @ApiOperation("编辑城市")
    public Result ModCity(City city){
        try {
            cityService.updateById(city);
            return Result.success("修改成功");
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/add")
    @ApiOperation("新增城市")
    public Result AddCity(City city){
        try{
            Date date = new Date();
            city.setCreateTime(date);
            boolean isAdd = cityService.save(city);
            if(isAdd){
                return Result.success("城市新增成功");
            }
            return Result.error("新增失败");
        }catch (Exception e){
            logger.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/view")
    @ApiOperation("城市详情")
    public Result cityView(Long cityId){
        try {
            City city = cityService.getById(cityId);
            QueryWrapper<CityImage> imageQuery = new QueryWrapper<>();
            imageQuery.eq("city_id",cityId);
            imageQuery.eq("is_delete",0);
            List<CityImage> images = cityImageService.list(imageQuery);
            List<String> imageList = new ArrayList<>();
            for(CityImage item : images){
                imageList.add(item.getImageUrl());
            }
            CityViewVO vo = new CityViewVO();
            BeanUtils.copyProperties(city,vo);
            vo.setReleaseTime(city.getModifyTime());
            vo.setImageList(imageList);
            return Result.success(vo);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

}

